package org.geometerplus.fbreader.library;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.geometerplus.android.fbreader.RLog;
import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase;
import org.geometerplus.fbreader.library.BooksDatabase;
import org.geometerplus.fbreader.library.FileInfoSet;
import org.geometerplus.fbreader.library.Book;
import org.geometerplus.fbreader.library.ImageUtil.BookSpec;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.image.ZLLoadableImage;
import org.zixuanstudio.ebook.R;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageManager;
import org.geometerplus.zlibrary.ui.android.view.ShelvesView;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;

public class BooksAdapter extends BaseAdapter {

	private static final String TAG = "BooksAdapter";

//	private ArrayList<BookSpec> mBooksData = new ArrayList<BookSpec>();

	private Context mContext;
	
	private ZLImage myImage;
	SyncImageLoader syncImageLoader;
	
	private ShelvesView  mListView;
	
	
	public BooksAdapter(Context context,ShelvesView  listView) {
		super();
		mContext = context;
		ImageUtil.initBooks(context);
		mListView = listView;
		syncImageLoader = SyncImageLoader.getInstance();
		initBooks();
		mListView.setOnScrollListener(onScrollListener);
	}

	private void initBooks() {
	
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return ImageUtil.mBooks.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.shelf_book, null);
		}
		ImageView bookCover = (ImageView)convertView.findViewById(R.id.book_cover);
		Book book = ImageUtil.mBooks.get(position);
		convertView.setTag(position);
		syncImageLoader.loadImage(position, book.File.getPath() , imageLoadListener);
		return convertView;
	}

	SyncImageLoader.OnImageLoadListener  imageLoadListener = new SyncImageLoader.OnImageLoadListener() {
		
		@Override
		public void onImageLoad(Integer t, Drawable drawable) {
			View view = mListView.findViewWithTag(t);
			if (view != null) {
				ImageView iv = (ImageView) view.findViewById(R.id.book_cover);
				if (drawable != null) {
				iv.setBackgroundDrawable(drawable);
				} else {
				iv.setBackgroundResource(R.drawable.default_book_cover);
				}
				
			}
		}
		
		@Override
		public void onError(Integer t) {
			View view = mListView.findViewWithTag(t);
			if (view != null) {
				ImageView iv = (ImageView) view.findViewById(R.id.book_cover);
				iv.setBackgroundResource(R.drawable.default_book_cover);
			}
		}
	};
	
	public void loadImage(){
		int start = mListView.getFirstVisiblePosition();
		int end =mListView.getLastVisiblePosition();
		if(end >= getCount()){
			end = getCount() -1;
		}
		syncImageLoader.setLoadLimit(start, end);
		syncImageLoader.unlock();
	}
	
	AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {
		
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
				case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
					syncImageLoader.lock();
					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
					loadImage();
					//loadImage();
					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					syncImageLoader.lock();
					break;
	
				default:
					break;
			}
			
		}
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// TODO Auto-generated method stub
			
		}
	};
	
}
